﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

using OviNotificationsApi;

namespace TestNotificationApplication
{
    public partial class MainForm : Form
    {
        Notifications oviNotificationsApi;
        public MainForm()
        {
            InitializeComponent();

            // Load defaults from config
            string restBase = ConfigurationManager.AppSettings["restBase"];
            string serviceId = ConfigurationManager.AppSettings["serviceId"];
            string serviceSecret = ConfigurationManager.AppSettings["serviceSecret"];
            string apiVersion = ConfigurationManager.AppSettings["apiVersion"];
            string appId = ConfigurationManager.AppSettings["appId"];

            restBaseTextBox.Text = restBase;
            serviceIdTextBox.Text = serviceId;
            serviceSecretTextBox.Text = serviceSecret;
            apiVersionTextBox.Text = apiVersion;
            jid_applicationIdTextBox.Text = appId;
            pingjid_applicationIdTextBox.Text = appId;
        }

        private void initializeButton_Click(object sender, EventArgs e)
        {
            if (oviNotificationsApi == null)
            {
                oviNotificationsApi =
                    new Notifications(
                        this.restBaseTextBox.Text,
                        this.apiVersionTextBox.Text,
                        this.serviceIdTextBox.Text,
                        this.serviceSecretTextBox.Text
                     );
            }
            else
            {
                oviNotificationsApi.BaseUrl = this.restBaseTextBox.Text;
                oviNotificationsApi.ApiVersion = this.apiVersionTextBox.Text;
                oviNotificationsApi.ServiceID = this.serviceIdTextBox.Text;
                oviNotificationsApi.ServiceSecret = this.serviceSecretTextBox.Text;
            }

            tabControl1.Enabled = true;
        }

        private void nid_PushButton_Click(object sender, EventArgs e)
        {
            try
            {
                string response = oviNotificationsApi.SendNotification(
                notificationIdTextBox.Text,
                nid_wakeUpCheckBox.Checked,
                nid_payloadTextBox.Text,
                nid_contentTypeTextBox.Text,
                nid_encodingTextBox.Text);

                responseTextBox.Text = response;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void jid_PushButton_Click(object sender, EventArgs e)
        {
            try
            {
                string response = oviNotificationsApi.SendNotification(
                jidTextBox.Text,
                jid_applicationIdTextBox.Text,
                jid_wakeUpCheckBox.Checked,
                jid_payLoadTextBox.Text,
                jid_contentTypeTextBox.Text,
                jid_encodingTextBox.Text);

                responseTextBox.Text = response;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pingNid_Button_Click(object sender, EventArgs e)
        {
            try
            {
                string response = oviNotificationsApi.PingClient(
                pingNotificationIdTextBox.Text);

                responseTextBox.Text = response;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pingjid_Button_Click(object sender, EventArgs e)
        {
            try
            {
                string response = oviNotificationsApi.PingClient(
                pingJidTextBox.Text,
                pingjid_applicationIdTextBox.Text);

                responseTextBox.Text = response;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void versionButton_Click(object sender, EventArgs e)
        {
            try
            {
                string response = oviNotificationsApi.Version();
                responseTextBox.Text = response;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pingServiceButton_Click(object sender, EventArgs e)
        {
            try
            {
                string response = oviNotificationsApi.PingService();
                responseTextBox.Text = response;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void queryLimitationsButton_Click(object sender, EventArgs e)
        {
            try
            {
                string response = oviNotificationsApi.QueryLimitations();
                responseTextBox.Text = response;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
